<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of wxmenu
 * 微信自定义菜单管理
 * @author xlp
 */
class wxmenu {

    protected static $appId = null;
    protected static $appSecret = null;
    protected static $menuData = null;
    protected static $aid = 0;
    static $token = null;
    static $errCode = 0;
    static $errorMsg = null;
    static $accessTokenPre = 'token/weixin/access_token_';

    static function init($aid, $appId, $appSecret) {
        self::$aid = $aid;
        self::$appId = $appId;
        self::$appSecret = $appSecret;
    }

    static function getErrorMsg() {
        return self::$errorMsg;
    }

    /**
     * 删除验证数据
     * @param string $appid
     */
    static public function resetAuth() {
        self::$token = '';
        S(self::$accessTokenPre . self::$appId, null);
        self::gettoken();
        return true;
    }

    /*
     * 检查是否需要重新获取accesstoken
     * 并记下日志
     */

    static public function checkAccessToken() {
        if (self::$errCode == 40001) {
            self::resetAuth();
            saveLog('accessToken/reset', 'weixin.menu');
            return true;
        }
        return false;
    }

    static function gettoken() {
        if (self::$token) {
            return self::$token;
        }
        self::$token = S(self::$accessTokenPre . self::$appId);
        if (!self::$token) {
            $result = getHttp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . self::$appId . '&secret=' . self::$appSecret);
            $result = json_decode($result, true);
            if ($result && !isset($result['errcode'])) {
                S(self::$accessTokenPre . self::$appId, $result['access_token'], $result['expires_in']);
                self::$token = $result['access_token'];
            } else {
                self::$errCode = $result['errcode'];
                self::$errorMsg = $result['errcode'];
                self::checkAccessToken();
                return false;
            }
        }
        return self::$token;
    }

    static function setmenu() {
        self::gettoken();
        $result = getHttp('https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . self::$token, self::$menuData);
        $result = json_decode($result, true);
        if ($result && !$result['errcode']) {
            return true;
        } else {
            self::$errorMsg = $result['errcode'] . '-' . $result['errmsg'];
            return false;
        }
    }

    static function getmenu() {
        self::gettoken();
        $result = getHttp('https://api.weixin.qq.com/cgi-bin/menu/get?access_token=' . self::$token);
        $result = json_decode($result, true);
        if ($result && !isset($result['errcode'])) {
            return $result;
        } else {
            self::$errorMsg = $result['errcode'] . '-' . $result['errmsg'];
            return false;
        }
    }

    static function delmenu() {
        self::gettoken();
        $result = getHttp('https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=' . self::$token);
        $result = json_decode($result, true);
        if ($result && !$result['errcode']) {
            return true;
        } else {
            self::$errorMsg = $result['errcode'] . '-' . $result['errmsg'];
            return false;
        }
    }

    static function setMenuData($menuData) {
//        $arr = C($this->menuConfig, 'button');
        $str = '';
        foreach ($menuData as $val) {
            if ($val['name']) {
                $str.='{"name":"' . $val['name'] . '"';
                if (!isset($val['type']) || $val['type'] == 'click') {
                    $str.=',"type":"click","sub_button":[';
                    foreach ($val['sub_button'] as $v) {
                        if ($v['name']) {
                            if ($v['type'] == 'view') {
                                $str.='{"type":"' . $v['type'] . '","name":"' . $v['name'] . '","url":"' . $v['url'] . '"},';
                            } else {
                                $str.='{"type":"' . $v['type'] . '","name":"' . $v['name'] . '","key":"' . $v['key'] . '"},';
                            }
                        }
                    }
                    $str = rtrim($str, ',') . '],"key":"' . $val['key'] . '"},';
                } else {
                    $str.=',"type":"view","url":"' . $val['url'] . '"},';
                }
            }
        }
        if (!$str) {
            return self::delmenu();
        }
        self::$menuData = '{"button":[' . rtrim($str, ',') . "]}";
//        print_r($menuData);
//        print_r(self::$menuData);
//        exit;
        return self::$menuData;
    }

}
